import {requestJsonData} from "./utils.js";

const getValType = (val) => {
    return Object.prototype.toString.call(val).replace("[object ", "").replace("]", "")
}

//标签数据
const flagData = {
    "餐厅": [
        {
            name: "AdminMange",
            flag: ["总部管理"],
            devPath: "http://admin.xiqueque.com",
            testPath: "https://admin.smartorderkiosk.com/"
        },
        {
            name: "AgentManage",
            flag: ["代理商管理"],
            devPath: "http://agent.xiqueque.com/",
            testPath: "https://agent.smartorderkiosk.com/"
        },
        {
            name: "SRMerchantFront",
            flag: ["商户端"],
            devPath: "http://mini.xiqueque.com/home",
            testPath: "https://client.smartorderkiosk.com"
        },
        {
            name: "SRFront",
            flag: ["客户端"],
            devPath: "http://k.xiqueque.com/#/welcome",
            testPath: "http://k.smartorderkiosk.com/#/welcome"
        },
        {name: "SRApp", flag: ["客户端APP"]}],
    "营销1.0": [
        {name: "95598-website", flag: ["95598网站"]},
        {name: "msb-app", flag: ["蒙速办"]},
        {name: "uni-mdej-app", flag: ["蒙电e家"]},
        {name: "wxpt-net", flag: ["微信公众号"]},
        {name: "kfdp-vue", flag: ["客服大屏"]},
        {name: "mdej-chat", flag: ["客服"]},
        {name: "yk_ydzypt", flag: ["移动营销"]}],
    "electron": [
        {name: "adb-client", flag: ["手机adb管理工具"]},
        {name: "UEMActivate", flag: ["手机提权工具"]},
        "AdbClient.zip", "Electron_Project"],
    "uniapp": [
        {name: "wx-uniapp", flag: ["个人小程序"]},
        "Uni_Demo", "Uni_Project"]
}

//项目文件夹数据
const projectData = await requestJsonData("/getProjectDir")
console.log("========projectData", projectData)

//将标签数据转换成树结构数据
function convertFlagDataToTreeData(data) {
    let treeList = []
    Object.entries(data).forEach(([key, value]) => {
        let children = []
        value.forEach((item) => {
            if (item.flag) {
                let newChildren = item.flag.map(item => {
                    return {label: item, value: item}
                })
                children = [...children, ...newChildren]
            }
        })
        let treeItem = {label: key, value: key, children: children};
        treeList.push(treeItem)
    })
    return treeList
}

//将标签数据对象转换成标签只有一个一级属性的对象
function convertFlagDataToFlagObj() {
    let flagDataObj = {}
    Object.entries(flagData).forEach(([key, value]) => {
        //遍历对象的属性
        value.forEach((item) => {
            let name = getValType(item) === "String" ? item : item.name
            let flag = getValType(item) === "String" ? [key] : [key, ...item.flag]
            let {devPath, testPath} = item
            flagDataObj[name] = flagDataObj[name] ? flagDataObj[name] : {}
            flagDataObj[name].flag = flagDataObj[name].flag ? flagDataObj[name].flag : []
            flagDataObj[name].flag = [...flagDataObj[name].flag, ...flag]
            flagDataObj[name].devPath = devPath
            flagDataObj[name].testPath = testPath
        })
    })
    return flagDataObj
}

// console.log("flagDataObj===", JSON.stringify(convertFlagDataToFlagObj()))

let flagDataObj = convertFlagDataToFlagObj()

//将层级对象数据转换成树数据
function convertDataToTreeData(data, parentKey = [], list = []) {
    Object.entries(data).forEach(([key, value]) => {
        if (getValType(value) === "Object") {
            let parentKeyList = [...parentKey, key]
            let children = convertDataToTreeData(value, parentKeyList)
            let {flag, devPath, testPath} = flagDataObj[key] ? flagDataObj[key] : {}
            flag = flag ? flag : []
            let treeItem = {id: parentKeyList.join(","), label: key, flag, devPath, testPath, children: children}
            list.push(treeItem)
        }
        if (getValType(value) === "Array") {
            let parentKeyList = [...parentKey, key]
            let children = value.map(item => {
                let {flag, devPath, testPath} = flagDataObj[item] ? flagDataObj[item] : {}
                flag = flag ? flag : []
                return {id: `${parentKeyList.join(",")},${item}`, label: item, flag, devPath, testPath}
            })
            let {flag, devPath, testPath} = flagDataObj[key] ? flagDataObj[key] : {}
            flag = flag ? flag : []
            let treeItem = {id: `${parentKeyList.join(",")}`, label: key, flag, devPath, testPath, children};
            list.push(treeItem)
        }
    })
    return list
}

//将数据转换成一维数组
function convertTreeDataToList(children, list = []) {
    children.forEach(item => {
        if (item.hasOwnProperty("children")) {
            list.push({...item, children: "undefined"})
            convertTreeDataToList(item.children, list)
        } else {
            list.push(item)
        }
    })
    return list
}

let mTreeList = convertDataToTreeData(projectData)
export let projectTreeList = mTreeList

export let projectList = convertTreeDataToList(mTreeList)

export let projectFlagTreeList = convertFlagDataToTreeData(flagData)

